﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using Bon.It.Server.Interfaces;
using Bon_It.Model;

namespace Bon.It.Server
{
    using Bon_It.Resources;

    /// <summary>
    /// The contractmanager class for the article selection
    /// </summary>
    public partial class ContractManager: IArticle
    {

        /// <summary>
        /// The Prefix for the Press EAN with the half tax.
        /// </summary>
        public const string PRESSCODELOWERTAX = "419";
        
        /// <summary>
        /// The prefix for the Press EAN with the full tax.
        /// </summary>
        public const string PRESSCODEFULLTAX = "414";

        #region Implementation of IArticle

        /// <summary>
        /// Get the article by the <see cref="Artikel.EANNummer"/>
        /// </summary>
        /// <param name="eanNumber"></param>
        /// <returns></returns>
        public Artikel GetArtikelByEan(long eanNumber)
        {
            //Check if the ean is a newspaper article number
            var result = IsNewsPaperArticle(eanNumber);

            if (result == null)
            {
                using (var context = GetDataContext())
                {
                    var item = context.Artikel.FirstOrDefault(a => a.EANNummer == eanNumber);
                    return item;
                }
            }   
             
            return result;
           
        }


        /// <summary>
        /// Determines that the <see cref="eanNumber"/> is an Newspaper article number.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is news paper article]; otherwise, <c>false</c>.
        /// </returns>
        private Artikel IsNewsPaperArticle(long eanNumber)
        {
            Artikel returnValue = null;
            if (eanNumber.ToString().Length.Equals(13))
            {
                if (eanNumber.ToString().StartsWith(PRESSCODEFULLTAX))
                {
                    returnValue = this.GetArticleById(419);
                    returnValue.Bezeichnung = StringResources.CustomerPaperFullTax;

                }
                else if (eanNumber.ToString().StartsWith(PRESSCODELOWERTAX))
                {
                    returnValue = this.GetArticleById(414);
                    returnValue.Bezeichnung = StringResources.CustomerPaperReducedTax;
                }

                if (returnValue != null)
                {
                    returnValue.Negative = false;
                    returnValue.EANNummer = this.ParseEAN(eanNumber);
                    returnValue.VKPreis = this.ParsePrice(eanNumber);
                }
            }
            return returnValue;
        }


        /// <summary>
        /// Parses the EAN from the press code
        /// </summary>
        /// <param name="eanNumber">The ean number.</param>
        /// <returns></returns>
        private long ParseEAN(long eanNumber)
        {
            long returnValue;

            string Ean = eanNumber.ToString().Substring(3, 5);
            
            if(long.TryParse(Ean, out returnValue))
                return returnValue;
            return 0;
        }
        
        /// <summary>
        /// Parses the price.
        /// </summary>
        /// <param name="eanNumber">The ean number.</param>
        /// <returns></returns>
        private decimal ParsePrice(long eanNumber)
        {
            decimal returnValue;

            string Ean = eanNumber.ToString().Substring(8, 4);

            if (decimal.TryParse(Ean, out returnValue))
                return returnValue / 1000;
            return 0;
            
        }

        public Artikel[] GetAllArticles()
        {
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<Artikel>(a => a.Warengruppe);
            dlo.LoadWith<Artikel>(a => a.Taxes);
            dlo.LoadWith<Artikel>(a => a.Gebinde);
            using (var context = GetDataContext(dlo))
            {
                return context.Artikel.Where(a=>a.CanBeModified).ToArray();
            }
        }

        public void PersistArticle(Artikel item)
        {
            using (var context = GetDataContext())
            {
                if (item.ID.Equals(0))
                {
                    context.Artikel.InsertOnSubmit(item);
                }else
                {
                    context.Artikel.Attach(item,true);
                }
                context.SubmitChanges();
            }
        }

        /// <summary>
        /// Delete the <see cref="Artikel"/> from the database.
        /// </summary>
        /// <param name="item"></param>
        public void DeleteArticle(Artikel item)
        {
            using (var context = GetDataContext())
            {
                context.Artikel.Attach(item);
                context.Artikel.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
        }

        /// <summary>
        /// Get the <see cref="Artikel"/> by the given id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Artikel GetArticleById(long id)
        {
            using (var context = GetDataContext())
            {
                return context.Artikel.FirstOrDefault(i => i.ID == id);
            }
        }

        #endregion
    }
}
